﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.UI.Common.Events;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
    public class OrgPresenter
    {
        private ExternalOrgDO _Org;
		private OrgView _View;
        private readonly IEventAggregator _EventAggregator;
		private SubscriptionToken _SubscriptionToken;

        public OrgPresenter(IEventAggregator eventAggregator)
        {
            this._EventAggregator = eventAggregator;
        }

		public OrgView View
		{
			get { return _View; }
			set { _View = value; }
		}

		private void OrgSelectedHandler(ExternalOrgDO org)
		{
			this._Org = org;
            this._View.Org = this._Org;
		}

        public void SubscribeOrgSelectedEvent(ExternalOrgDO org)
        {
            this._Org = org;
            OrganizationSelectedEvent orgSelectedEvent =
                _EventAggregator.GetEvent<OrganizationSelectedEvent>();
            if (this._SubscriptionToken != null)
            {
                orgSelectedEvent.Unsubscribe(_SubscriptionToken);
            }
            this._SubscriptionToken = orgSelectedEvent.Subscribe(
                OrgSelectedHandler,
                ThreadOption.UIThread,
                false);
        }
    }
}
